import './case.less'
import workbenchMixin from '@/mixins/workbench'

export default {
    mixins: [workbenchMixin],
    data() {
        return {
            // 申请退回
            returnCaseLayer: {
                id: 0,
                show: false,
                title: '申请退回',
                tips: '是否确认申请案件退回？'
            },
            // 申请收回
            recoverCaseLayer: {
                id: 0,
                show: false,
                title: '申请收回',
                tips: '是否确认申请案件收回？'
            },
            // 分派案件
            distributeCaseLayer: {
                show: false,
                title: '分派案件',
                form: {
                    idList: 0
                }
            },
            // 申请结案
            closeCaseLayer: {
                show: false,
                title: '申请结案',
                ref: 'closeCaseForm',
                form: {
                    idList: 0
                },
                uploadUrl: api.API_BASE_URL + '/upload',
                fileList: [],
                fileNames: [],
                caseType: 0      // 普通案件不显示
            },
            // 申请暂停
            pauseCaseLayer: {
                show: false,
                title: '申请暂停',
                ref: 'pauseCaseForm',
                form: {
                    id: 0
                }
            },
            // 申请转案
            changeCaseLayer: {
                id: 0,
                show: false,
                title: '申请转案',
                ref: 'changeCaseForm',
                form: {
                    id: 0
                }
            },
            tipsLayer: {
                show: false,
                title: '提示',
                tips: '',
                flag: true,
                type: 1       // 1:分派 2:结案 3:暂停 4:转案
            }
        }
    },
    mounted() {
        this.$store.dispatch('setBreadData', [
            {
                name: '工作台'
            },
            {
                name: '我的案件'
            }
        ])
        this.getTableData(1)
    },
    methods: {
        // 申请退回
        showReturnCaseLayer(id) {
            this.returnCaseLayer.show = true
            this.returnCaseLayer.id = id
        },
        // 提交申请退回
        returnCaseSave() {
            api.sendBackCase({ id: this.returnCaseLayer.id }).then(res => {
                this.$msg.success('申请退回成功')
                this.tipsLayer.show = false
                this.returnCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 申请收回
        showRecoverCaseLayer(id) {
            this.recoverCaseLayer.show = true
            this.recoverCaseLayer.id = id
        },
        // 提交申请收回
        recoverCaseSave() {
            api.takeBackCase({ id: this.recoverCaseLayer.id }).then(res => {
                this.$msg.success('申请收回成功')
                this.tipsLayer.show = false
                this.recoverCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 显示分派案件
        showDistributeCaseLayer(id, caseType) {
            this.distributeCaseLayer.show = true
            this.distributeCaseLayer.form = {}
            this.distributeCaseLayer.form.idList = id
            // this.getRoleList()
            let _type = 2
            switch (caseType) {
                case 1:
                    _type = 2
                    break
                case 2:
                    _type = 5
                    break
                case 3:
                    _type = 6
                    break
            }
            this.queryRoleList(_type)
        },
        // 提交分派案件
        distributeCaseSave() {
            this.distributeCaseLayer.form.targetId = this.targetForm.id
            api.allotSelectedCase(this.distributeCaseLayer.form).then(res => {
                this.$msg.success('分派案件成功')
                this.tipsLayer.show = false
                this.distributeCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 显示申请结案
        showCloseCaseLayer(data) {
            this.closeCaseLayer.show = true
            this.closeCaseLayer.form = {}
            this.closeCaseLayer.form.id = data.id
            this.closeCaseLayer.caseType = data.caseType
            this.closeCaseLayer.fileList = []
            console.log(this.closeCaseLayer)
            // this.getRoleList()
            this.queryRoleList(4)
        },
        // 上传文件之前验证格式
        beforeUpload(file) {
            if (file.size == 0) {
                this.$msg.error('上传文件大小不能为0B!')
                return false
            }
            // // const isIamge = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif';
            // if (file.size == 0) {
            //     this.$msg.error('上传文件大小不能为0B!')
            //     return false
            // }
            // const limitSize = file.size / 1024 / 1024 < 100;

            // // if (!isIamge) {
            // //     this.$msg.error('上传文件只能是 JPG png gif格式!');
            // // }
            // if (!limitSize) {
            //     this.$msg.error('上传文件大小不能超过 100MB!')
            // }
            // // return isIamge && limitSize;
            // return limitSize;
        },
        // 文件列表移除文件时的钩子	
        removeUploadFile(file, fileList) {
            this.closeCaseLayer.fileList = fileList
        },
        // 上传文件成功
        uploadSuccess(res, file) {
            var _res = file.response;
            if (_res.code == 10000) {
                let _obj = {}
                _obj.name = file.name
                _obj.url = _res.data.url
                this.closeCaseLayer.fileList.push(_obj)
            }
            else {
                this.$msg.error(_res.msg);
            }
        },
        // 提交申请结案
        closeCaseSave() {
            let _data = this.closeCaseLayer.form
            _data.targetId = this.targetForm.id
            _data.urlList = []
            this.closeCaseLayer.fileList.forEach(item => {
                let _obj = {}
                _obj.fileName = item.name
                _obj.url = item.url
                _data.urlList.push(_obj)
            })
            api.closeCase(_data).then(res => {
                this.$msg.success('申请结案成功')
                this.tipsLayer.show = false
                this.closeCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 显示申请暂停
        showPauseCaseLayer(id) {
            this.pauseCaseLayer.show = true
            this.pauseCaseLayer.form = {}
            this.pauseCaseLayer.form.id = id
            this.queryRoleList(3)
        },
        // 提交申请暂停
        pauseCaseSave() {
            this.pauseCaseLayer.form.targetId = this.targetForm.id
            api.suspendCase(this.pauseCaseLayer.form).then(res => {
                this.$msg.success('申请暂停成功')
                this.tipsLayer.show = false
                this.pauseCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 显示申请转案
        showChangeCaseLayer(id) {
            this.changeCaseLayer.show = true
            this.changeCaseLayer.form = {}
            this.changeCaseLayer.form.id = id
            // this.getRoleList()
            this.queryRoleList(3)
        },
        // 提交申请转案
        changeCaseSave() {
            this.changeCaseLayer.form.targetId = this.targetForm.id
            api.switchCase(this.changeCaseLayer.form).then(res => {
                this.$msg.success('申请转案成功')
                this.tipsLayer.show = false
                this.changeCaseLayer.show = false
                this.getTableData(this.tableData.index)
                this.$store.dispatch('getUnProcess')
            }).catch(res => {
                console.log(res)
            })
        },
        // 提示弹窗
        showTipsLayer(type) {
            if (type == 1) {
                if (!this.targetActive) {
                    this.targetForm.id = this.userInfo.id
                }
                if (!this.targetForm.id) {
                    this.$msg.error('请选择分派对象')
                    return false
                }
                this.tipsLayer.tips = '是否确认要分派案件?'
            }
            else if (type == 2) {
                // if (this.closeCaseLayer.fileList && !this.closeCaseLayer.fileList.length) {
                //     this.$msg.error('请上传结案报告')
                //     return false
                // }
                if (!this.targetActive) {
                    this.targetForm.id = this.userInfo.id
                }
                if (!this.targetForm.id) {
                    this.$msg.error('请选择审核人')
                    return false
                }
                this.tipsLayer.tips = '是否确认要结案?'
            }
            else if (type == 3) {
                if (!this.pauseCaseLayer.form.suspendCause) {
                    this.$msg.error('请填写原因')
                    return false
                }
                if (!this.targetActive) {
                    this.targetForm.id = this.userInfo.id
                }
                if (!this.targetForm.id) {
                    this.$msg.error('请选择审核人')
                    return false
                }
                this.tipsLayer.tips = '是否确认要暂停案件?'
            }
            else if (type == 4) {
                if (!this.changeCaseLayer.form.caseType) {
                    this.$msg.error('请选择案件类型')
                    return false
                }
                if (!this.changeCaseLayer.form.switchCause) {
                    this.$msg.error('请填写原因')
                    return false
                }
                if (!this.targetActive) {
                    this.targetForm.id = this.userInfo.id
                }
                if (!this.targetForm.id) {
                    this.$msg.error('请选择审核人')
                    return false
                }
                this.tipsLayer.tips = '是否确认要转案?'
            }
            this.tipsLayer.show = true
            this.tipsLayer.type = type
        },
        // 确认提交
        confirmSave() {
            // 分派
            if (this.tipsLayer.type == 1) {
                this.distributeCaseSave()
            }
            // 结案
            else if (this.tipsLayer.type == 2) {
                this.closeCaseSave()
            }
            // 暂停
            else if (this.tipsLayer.type == 3) {
                this.pauseCaseSave()
            }
            // 转案
            else if (this.tipsLayer.type == 4) {
                this.changeCaseSave()
            }
        }
    }
}